package com.ks.pc.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.PaginationDto;
import com.ks.dto.ProductDto;
import com.ks.pc.dao.DaoSupport;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.SysProductService;

@Service("sysProductService")
public class SysProductServiceImpl implements SysProductService {

	public static final Logger logger = Logger.getLogger(SysProductServiceImpl.class);

	@Autowired
	private CommonService commonService;
	@Autowired
	private DaoSupport daoSupport;

	@Override
	public List<ClassifyDto> findClassifyDto() {
		try {
			return daoSupport.findForList("ProductMapper.findClassifyDto", null);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	/**
	 * 组装参数：</br>
	 * 1分页对象给limit用</br>
	 * 2模糊查询key</br>
	 * 
	 * @param page
	 * @param rows
	 * @param searchTxt
	 * @return
	 */
	private Map<String, Object> wrapParams(Integer page, Integer rows, String searchTxt) {
		PaginationDto<ClassifyDto> pageDto = new PaginationDto<ClassifyDto>(page, rows);
		RowBounds rowBounds = new RowBounds(pageDto.getOffset(), pageDto.getLimit());
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("rowBounds", rowBounds);
		if (null != searchTxt) {
			param.put("searchTxt", searchTxt);
		}
		return param;
	}
	// TODO del
	@Override
	public PaginationDto<ClassifyDto> queryClassifyBySearchTxt(Integer page, Integer rows, String searchTxt) {
		PaginationDto<ClassifyDto> pageDto = new PaginationDto<ClassifyDto>(page, rows);
		Map<String, Object> param = wrapParams(page, rows, searchTxt);
		List<ClassifyDto> dtos;
		try {
			Integer count = daoSupport.findForObject("CommonMapper.findClassifyByConditionCount", param);
			dtos = commonService.queryClassifyBySearchTxt(param);
			pageDto.setRecords(count);
			pageDto.setRows(dtos);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return pageDto;
	}

	// TODO del
	public PaginationDto<BrandDto> findBrandGrid(PaginationDto<BrandDto> pageDto, Map<String, Object> param) {
		List<BrandDto> dtos = null;
		try {
			Integer count = daoSupport.findForObject("ProductMapper.findBrandGridCount", param);
			dtos = daoSupport.findForList("ProductMapper.findBrandGrid", param);
			pageDto.setRecords(count);
			pageDto.setRows(dtos);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return pageDto;
	}

	public String saveDto(Map<String, Object> param) {
		try {
			Object pkId = daoSupport.save("ProductMapper.saveDto", param);
			return pkId.toString();
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public ClassifyDto findClassifyById(String classifyId) {
		try {
			return daoSupport.findForObject("ProductMapper.findClassifyById", classifyId);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public boolean updateDtoById(String sqlMapper, Object dto) {
		try {
			return ((Integer) daoSupport.update(sqlMapper, dto) == 1) ? true : false;
		} catch (Exception e) {
			logger.debug(e.getMessage());
			return false;
		}
	}

	public BrandDto findBrandById(String brandId) {
		try {
			return daoSupport.findForObject("ProductMapper.findBrandById", brandId);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public int countProductByClassifyId(Map<String, Object> param) {
		try {
			return daoSupport.findForObject("ProductMapper.countProductByClassifyId", param);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return 0;
	}

	public List<ProductDto> findProductByClassifyId(Map<String, Object> param) {
		try {
			return daoSupport.findForList("ProductMapper.findProductByClassifyId", param);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public List<ProductDto> findProductByName(Map<String, Object> param) {
		try {
			return daoSupport.findForList("ProductMapper.findProductByName", param);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public ProductDto findProductById(Integer productId) {
		try {
			return daoSupport.findForObject("ProductMapper.findProductById", productId);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return null;
	}

	public PaginationDto<ProductDto> findProductGrid(PaginationDto<ProductDto> pageDto, Map<String, Object> param) {
		List<ProductDto> dtos = null;
		try {
			dtos = daoSupport.findForList("ProductMapper.findProductGrid", param);
			pageDto.setRecords(dtos.size());
			pageDto.setRows(dtos);
		} catch (Exception e) {
			logger.debug(e.getMessage());
		}
		return pageDto;
	}

	public boolean deleteProductByIds(String idsArr) {
		String[] split = StringUtils.split(idsArr);
		List<String> ids = Arrays.asList(split);
		// if(split.length <= 0){return false;}
		// List<Object> ids = new ArrayList<Object>();
		// for (int i = 0; i < split.length; i++) {
		// ids.add(Integer.parseInt(split[i]));
		// }
		try {
			daoSupport.delete("ProductMapper.deleteProductByIds", ids);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public Integer getMaxProductId() {
		try {
			return daoSupport.findForObject("ProductMapper.getMaxProductId", null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<String> findBannerById(Integer classifyId) {
		try {
			return daoSupport.findForList("ProductMapper.findBannerById", classifyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean deleteBrandByIds(String idsArr) {
		String[] split = StringUtils.split(idsArr);
		List<String> ids = Arrays.asList(split);
		try {
			daoSupport.delete("ProductMapper.deleteBrandByIds", ids);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<ClassifyDto> findClassifyAll() {
		try {
			return daoSupport.findForList("ProductMapper.findClassifyAll", null);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// @Override
	// public List<BrandDto> findBrandAllByClassifyId(String classifyId) {
	// return null;
	// }

	@Override
	public List<Map<String, Object>> classifyBrandRel() {
		List<Map<String, Object>> rsMap = new ArrayList<Map<String, Object>>();

		try {
			List<ClassifyDto> classifys = daoSupport.findForList("ProductMapper.findClassifyAll", null);
			for (ClassifyDto classifyDto : classifys) {
				Integer id = classifyDto.getId();
				if (id != 0) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("classifyId", id);
					map.put("classifyName", classifyDto.getName());
					List<BrandDto> brandDtos = daoSupport.findForList("ProductMapper.findBrandAllByClassifyId", id);
					if (null != brandDtos) {
						map.put("brandDtos", brandDtos);
					}
					rsMap.add(map);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return rsMap;
	}


}
